package moo.stargalaxy_bukkit.api;

import com.alibaba.fastjson.JSONObject;
import moo.stargalaxy_bukkit.utils.HttpUtil;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.Inventory;
import java.util.UUID;
import org.bukkit.Bukkit;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;

public class GalaxyAPI {

    public static String url = "";
    public static String key = "";

    public static boolean isConnect(JSONObject jsonObject){
        if(jsonObject.getInteger("code") != 1){
            //  || !jsonObject.getString("msg").equals("Success")
            return false;
        }
        return true;
    }

    public static boolean canCross(Player player, JSONObject jsonObject){
        //String string2 = jsonObject.getJSONObject("datas").getString("显示名称");
        //String string1 = jsonObject.getJSONObject("datas").getString("连接地址");
        //String string = jsonObject.getJSONObject("datas").getString("分配端口");
        return jsonObject.containsKey("datas");
    }

    public static JSONObject sendGet(String type){
        return HttpUtil.sendGet(url, type, "UTF-8", key);
    }

    // 子服请求跨服时，让蹦极跨服插件进行访问接口进行验证是否允许跨服
    // /fun_crossserverfid_normal&server_fid=<服务器识别码>
    public static JSONObject getCrossServerFid(String serverID){
        return sendGet("/fun_crossserverfid_normal?server_fid=" + serverID);
    }

    // 获取全部可用子服模板
    // /get_serevertemplates
    public static JSONObject getServerTemplates(){
        return sendGet("/get_serevertemplates");
    }

    public static JSONObject restartServer(String server){
        return sendGet("/fun_restartserver?server_fid=" + server);
    }

    public static JSONObject startServer(String server){
        return sendGet("/fun_startserver?server_fid=" + server);
    }

    public static JSONObject stopServer(String server){
        return sendGet("/fun_stopserver?server_fid=" + server);
    }


    //
    // 创建玩家专属私人子服
    public static JSONObject createServer(UUID uuid, String player, String server, int max){
       return sendGet("/fun_createserver?user_uuid="+ uuid + "&user_name=" + player.replaceAll("§[0-9]|§[a-z]","") + "&template_file=" + server + "&user_max_create=" + max);
    }

    // /fun_deleteserver?server_fid=<服务器识别码>
    // 删除子服
    public static JSONObject deleteServer(String server){
        return sendGet("/fun_deleteserver?server_fid=" + server);
    }

    // /fun_verifiuserserver?user_uuid=<玩家UUID>&server_fid=<服务器识别码>
    // 验证特定子服是否对应玩家
    public static JSONObject verifyUserServer(UUID uuid, String server){
        return sendGet("/fun_verifiuserserver?user_uuid=" + uuid + "&server_fid=" + server);
    }

    // /sub_fidrundatas_plugins?server_fid=<服务器识别码>&online_player=<在线人数>&max_player=<最大在线人数>&runtime_tps=<当前实时TPS>&joinlimit_tps=<限制低的进入TPS>
    // 【内置功能】插件循环向控制台提交当前服务器运行信息
    // 子服插件实时回馈运行数据
    public static JSONObject gitServerData(String server, String playerOnline, String maxPlayer, String tps, String limitLowTPS){
        return sendGet("/sub_fidrundatas_plugins?server_fid=" + server + "&online_player=" + playerOnline + "&max_player=" + maxPlayer + "&runtime_tps=" + tps + "&joinlimit_tps=" + limitLowTPS);
    }

    //通知控制台修改当前子服特定数据
    // /sub_fidedits_plugins?server_fid=<服务器识别码>&data_type=<数据项目>&data_record=<新数据内容>
    public static JSONObject gitFixServerSpecialData(String server, String dataType, String objects){
        // 名字存在中文.必须URL编码后避免异常
        String objects_encodedUrl = java.net.URLEncoder.encode(objects);
        return sendGet("/sub_fidedits_plugins?server_fid=" + server + "&data_type=" + dataType + "&data_record=" + objects_encodedUrl);
    }
    // /get_userservers?user_uuid=<玩家UUID>
    // 获取玩家拥有的全部子服
    public static JSONObject getPlayerServer(String uuid_fromt){
        return sendGet("/get_userservers?user_uuid=" + uuid_fromt);
    }


    // /get_userservers_sel?user_uuid=<玩家UUID>&sel=<排序序号>
    // 获取玩家特定序号的服务器信息
    public static JSONObject getPlayerServer_sel(String uuid_fromt , String number){
        return sendGet("/get_userservers_sel?user_uuid=" + uuid_fromt + "&sel=" + number);
    }

    // /get_serverinfo?server_fid=<子服识别码>
    // 获取特定子服下全部数据
    public static JSONObject getServerInfo(String server){
        return sendGet("/get_serverinfo?server_fid=" + server);
    }


    // /fun_useservers_sendcommand?server_fid=<服务器识别码>&command_data=<命令内容>
    // 发送命令到特定群星子服务器
    public static JSONObject sendFidserverCommand(String server , String commandtext){
        // 内容存在中文等符号.必须URL编码后避免异常
        String objects_encodedUrl = java.net.URLEncoder.encode(commandtext);
        return sendGet("/fun_useservers_sendcommand?server_fid=" + server + "&command_data=" + objects_encodedUrl);
    }


    // /fun_allservers_sendcommand?&command_data=<命令内容>
    // 发送命令到全部群星子服务器
    public static JSONObject sendAllserverCommand(String commandtext){
        // 内容存在中文等符号.必须URL编码后避免异常
        String objects_encodedUrl = java.net.URLEncoder.encode(commandtext);
        return sendGet("/fun_allservers_sendcommand?command_data=" + objects_encodedUrl);
    }

}
